home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / TCPSource / binsubs.c next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  7.5 KB  |  409 lines  |  [TEXT/MPS ]

  1. #ifndef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    binsubs.c
  6. *
  7. *    MacBinary Subroutines.    
  8. *
  9. *    Called by:
  10. *        bkgr.c
  11. */
  12.  
  13. #include "stdio.h"
  14. #include "memory.h"
  15. #include "fcntl.h"
  16. #include "mpw.h"
  17. #include "whatami.h"
  18.  
  19. #include "Windows.h"
  20. #include "Files.h"
  21. #include "Packages.h"
  22.  
  23. #include "MacBinary.h"
  24.  
  25.  
  26. char *strncpy();
  27.  
  28. #define BLOCKS(x)    ((x+127)/128)
  29. #define lmove(f,t)    movmem(f,t,4)
  30.  
  31. MBHead
  32.     *mbh;
  33. char 
  34.     buffer[128];
  35.  
  36. char *strsave( p)
  37. char *p;
  38. {
  39.     char *t;
  40.  
  41.     t= (char *)NewPtr(strlen(p));
  42.     strncpy(t,p,strlen(p));
  43.     
  44.     return(t);
  45. }
  46.  
  47. GetFileInfo(vol,name,iop)
  48. short vol;
  49. char *name;
  50. FileParam *iop;
  51. {
  52.     iop->ioNamePtr = name;
  53.     iop->ioVRefNum=vol;
  54.     iop->ioFVersNum=iop->ioFDirIndex=0;
  55.     PBGetFInfo((ParmBlkPtr) iop, FALSE);
  56. }
  57.  
  58. SetFileInfo(vol,name,iop)
  59. short vol;
  60. char *name;
  61. FileParam *iop;
  62. {
  63.     iop->ioNamePtr = name;
  64.     iop->ioVRefNum=vol;
  65.     iop->ioFVersNum=iop->ioFDirIndex=0;
  66.     PBSetFInfo( (ParmBlkPtr) iop, FALSE);
  67. }
  68.  
  69. MakeTextFile(vol,name,iop)
  70. short vol;
  71. char *name;
  72. FileParam *iop;
  73. {    GetFileInfo(vol,name,iop);
  74.     iop->ioFlFndrInfo.fdType='TEXT';
  75.     iop->ioFlFndrInfo.fdCreator='EDIT';
  76.     SetFileInfo(vol,name,iop);
  77. }
  78.  
  79. isMacBinary(p)
  80. MBHead *p;
  81. {
  82.     return( (p->nlen > 0)   &&
  83.             (p->nlen < 65)  &&
  84.             (p->zero1 == 0) &&
  85.             (p->zero2 == 0) &&
  86.             (p->zero3 == 0));
  87. }
  88.  
  89. MBsize( mbfp )
  90. MBFile *mbfp;
  91. {
  92.     long int size;
  93.     int ret;
  94.     
  95.     size = 0;
  96.     
  97.     ret = GetEOF( mbfp->fd, &size );            /* length of file data fork */
  98.     if (ret != noErr) 
  99.         size = 0;
  100.  
  101.     return(size);
  102. }
  103.  
  104. MBFile *MBopen( file, vrefnum, mode)
  105. char *file;
  106. short vrefnum,mode;
  107. {
  108.     MBFile *mbfp;
  109.     int err;
  110.  
  111.     if (mode & MB_WRITE)
  112.         putln("MBOpen for write");
  113.     else 
  114.         putln("MBOpen for read");
  115.  
  116.     if (mode & MB_DISABLE)
  117.         putln("MacBinary Protocol Disabled");
  118.  
  119.     mbfp= (MBFile *)NewPtr( sizeof(MBFile));
  120.     if (mbfp==0L)
  121.         return(0L);
  122.     movmem( file, mbfp->name,64);
  123.     putln(mbfp->name);
  124.     c2pstr(mbfp->name);
  125.     mbfp->vrefnum=vrefnum;
  126.     mbfp->mode = mode;
  127.  
  128.     if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
  129.         if ((err==-43) && (mode & MB_WRITE)) {
  130.             Create( mbfp->name, vrefnum, '????','TEXT');
  131.             if (FSOpen( mbfp->name, vrefnum, &mbfp->fd)) 
  132.                 return( 0L);
  133.             }
  134.         else 
  135.             return(0L);
  136.         }
  137.  
  138.     mbfp->binary=0;
  139.     mbfp->sector1=1;
  140.     mbfp->fork=0;
  141.     return( mbfp);
  142. }
  143.  
  144. MBwrite( out, buffer, size)
  145. MBFile *out;
  146. int size;
  147. char *buffer;
  148. {
  149.     int rsize;
  150.     
  151.     if (size < 1)
  152.         return(0);
  153.  
  154.     rsize=size;
  155.  
  156.     if (out->sector1 && (size >= sizeof(struct MBHead)) &&
  157.         isMacBinary(buffer) && (!(out->mode & MB_DISABLE))) {
  158.         putln("First sector of MacBinary file");
  159.         ProcessMBHead( out, buffer);
  160.         buffer+=128;
  161.         if ((size-=128) <1)
  162.             return(rsize);
  163.         }
  164.  
  165.     if (bwrite( out,buffer,size))
  166.         return(-1);
  167.     else
  168.         return( rsize);
  169. }
  170.  
  171. bwrite( out, buffer, size)
  172. MBFile *out;
  173. int size;
  174. char *buffer;
  175. {
  176.     long len=size;
  177.     int error;
  178.  
  179.     error = 0;
  180.     
  181.     if (out->binary) {
  182.         if (out->bytes>0) {
  183.             if (out->bytes < len) len = out->bytes;
  184.             error= FSWrite( out->fd, &len, buffer);
  185.             out->bytes -= len;
  186.             buffer +=len;
  187.             size -= (int)len;
  188.             }
  189.         if (out->bytes<= 0) {
  190.             if (!out->fork) {
  191.                 out->fork=1;
  192.                 out->bytes=BLOCKS(out->rlen)*128;
  193.                 SetEOF( out->fd, (long) out->dlen);
  194.                 FSClose( out->fd);
  195.                 if (out->bytes) {
  196.                     OpenRF( out->name, out->vrefnum,&out->fd);
  197.                     if (size) {
  198.                         len = (long) size;
  199.                         error= FSWrite( out->fd, &len, buffer);
  200.                         }
  201.                     }
  202.                 else
  203.                     out->fd=0;
  204.                 }
  205.             else SetEOF( out->fd, (long) out->rlen);
  206.             }
  207.         }
  208.     else {
  209.         error = FSWrite( out->fd, &len, buffer);
  210.         }
  211.     return (error);
  212. }
  213.  
  214. MBclose( out)
  215. MBFile *out;
  216. {
  217.     FileParam finfo;
  218.     long fpos;
  219.  
  220.     putln("MBclose");
  221.     
  222.     if (!out->fd)
  223.         return;
  224.  
  225.     if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
  226.         if (out->fork)
  227.             SetEOF( out->fd, (long) out->rlen);
  228.         else
  229.             SetEOF( out->fd, (long) out->dlen);
  230.  
  231.         FSClose( out->fd);
  232.         GetFileInfo( 0, out->name, &finfo);
  233.     
  234.         movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
  235.         lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
  236.         lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
  237.         finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
  238.         finfo.ioFlRLgLen=out->rlen;
  239.         finfo.ioFlLgLen =out->dlen;
  240.     
  241.         SetFileInfo( 0, out->name, &finfo);
  242.         }
  243.     else if (out->mode & MB_WRITE) {
  244.         GetFPos( out->fd, &fpos);
  245.         SetEOF(  out->fd,  fpos);
  246.         FSClose( out->fd);
  247.         }
  248.     else
  249.         FSClose( out->fd);
  250. }
  251.  
  252. ProcessMBHead( out, header)
  253. MBFile *out;
  254. MBHead *header;
  255. {
  256.     int err;
  257.  
  258.     movmem( header, &out->header, sizeof(MBHead));
  259.     out->binary=1;
  260.     lmove( &header->dflen[0], &out->dlen);
  261.     lmove( &header->rflen[0], &out->rlen);
  262.     out->bytes= BLOCKS(out->dlen)*128;
  263.     out->fork=0;
  264.     out->sector1=0;
  265.  
  266.     FSClose(out->fd);
  267.     if (FSDelete( out->name, out->vrefnum))
  268.         putln("Error Deleting Old File ");
  269.         
  270.     movmem( &out->header.nlen, out->name,63);
  271.     
  272.     MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
  273.  
  274.     if (out->bytes) {
  275.         if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
  276.             if (err=-43) {
  277.                 long cre,typ;
  278.  
  279. /*  this crashes Mac Pluses                
  280.                 typ = *(long *)out->header.type;
  281.                 cre = *(long *)out->header.creator;
  282. */
  283.                 lmove(out->header.type, &typ);
  284.                 lmove(out->header.creator, &cre);
  285.  
  286.                 Create( out->name, out->vrefnum, cre,typ);
  287.                 if (FSOpen( out->name, out->vrefnum, &out->fd)) 
  288.                     return( 0L);
  289.                 }
  290.             else {
  291.                 return(0L);
  292.                 }
  293.             }
  294.         }
  295.     else {
  296.         if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
  297.             if (err=-43) {
  298.                 long cre,typ;
  299.                 
  300. /*  this crashes Mac Pluses                
  301.                 typ = *(long *)out->header.type;
  302.                 cre = *(long *)out->header.creator;
  303. */
  304.                 lmove(out->header.type, &typ);
  305.                 lmove(out->header.creator, &cre);
  306.  
  307.                 Create( out->name, out->vrefnum, cre,typ);
  308.                 if (OpenRF( out->name, out->vrefnum, &out->fd)) 
  309.                     return( 0L);
  310.                 }
  311.             else {
  312.                 return(0L);
  313.                 }
  314.             }
  315.         out->fork = 1;
  316.         out->bytes=BLOCKS(out->rlen)*128;
  317.         }
  318. }
  319.  
  320. MBread( in, buffer, size)
  321. MBFile *in;
  322. int size;
  323. char *buffer;
  324. {
  325.     char *p;
  326.     int rsize=size;
  327.  
  328.  
  329.     if (in->fork<0) {
  330.         return(-1);
  331.         }
  332.  
  333.     p=buffer;
  334.     if (in->sector1) {
  335.         FileParam finfo;
  336.  
  337.         setmem( &in->header, sizeof(MBHead), 0);
  338.         movmem(  in->name, &in->header.nlen, 64);
  339.         GetFileInfo( in->vrefnum, in->name, &finfo);
  340.         movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
  341.         in->header.protected = (in->header.zero2 & 0x40)?1:0;
  342.         in->header.zero2 = 0;
  343.         lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
  344.         lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
  345.         lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
  346.         lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
  347.         in->dlen=finfo.ioFlLgLen;
  348.         in->rlen=finfo.ioFlRLgLen;
  349.         if (! (in->mode & MB_DISABLE) ) {
  350.             if (size<128) return(-1);
  351.  
  352.             movmem( &in->header, p, 128);
  353.             p +=128;
  354.             size -= 128;
  355.             in->bytes= BLOCKS(in->dlen)*128;
  356.             in->binary=1;
  357.             }
  358.         else {
  359.             in->bytes = in->dlen;
  360.             in->rlen=0;
  361.             in->binary=0;
  362.             }
  363.         in->sector1=0;
  364.         MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
  365.         }
  366.  
  367.     if ( size >0) {
  368.         long length = (long)size;
  369.         int err;
  370.  
  371.         err = FSRead( in->fd, &length, p);
  372.  
  373.         size -=(int)length;
  374.         in->bytes -=length;
  375.         p += length;
  376.  
  377.         if (err == -39 || (in->bytes<=0) ) {
  378.             FSClose( in->fd );
  379.             if (in->bytes<0L) in->bytes=0L;
  380.             size -= (int)in->bytes;
  381.             p    +=      in->bytes;                /* Make adjustments for necessary 128 byte term */
  382.             if (!in->fork ) {
  383.                 in->fork=1;
  384.                 in->bytes= BLOCKS(in->rlen)*128;
  385.                 if (in->bytes) {
  386.                     OpenRF( in->name, in->vrefnum, &in->fd);
  387. #ifdef READ
  388.                     length=(long)size;
  389.                     if (length >0L) {
  390.                         err = FSRead( in->fd, &length, p);
  391.                         size -= (int)length;
  392.                         in->bytes -=length;
  393.                         }
  394. #endif READ
  395.                     }
  396.                 else {
  397.                     in->fd=0;
  398.                     in->fork=-1;                    /* Time to close up shop */
  399.                     }
  400.                 }
  401.             else {
  402.                 in->fd=0;
  403.                 in->fork=-1;                    /* Time to close up shop */
  404.                 }
  405.             }
  406.         }
  407.     return( rsize-size); 
  408. }
  409.